package com.wust.smartcity.generatorcode.task;

import com.wust.smartcity.generatorcode.entity.ColumnInfo;
import com.wust.smartcity.generatorcode.task.base.AbstractTask;
import com.wust.smartcity.generatorcode.utils.*;
import freemarker.template.TemplateException;

import java.io.File;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author ：wust
 * @date：2019-12-26
 */
public class EntityTask extends AbstractTask {

    /**
     * 1.单表生成  2.多表时生成子表实体
     */
    public EntityTask(String tableName,String className, List<ColumnInfo> infos) {
        this(tableName,className, null, null, infos);
    }

    /**
     * 一对多关系生成主表实体
     */
    public EntityTask(String tableName,String className, String parentClassName, String foreignKey, List<ColumnInfo> tableInfos) {
        this(tableName,className, parentClassName, foreignKey, null, tableInfos);
    }

    /**
     * 多对多关系生成主表实体
     */
    public EntityTask(String tableName,String className, String parentClassName, String foreignKey, String parentForeignKey, List<ColumnInfo> tableInfos) {
        super(tableName,className, parentClassName, foreignKey, parentForeignKey, tableInfos);
    }

    @Override
    public void run() throws IOException, TemplateException {
        String entityPackageName = ConfigUtil.getConfiguration().getPath().getEntityPackageName();
        String filePath = StringUtil.package2Path(ConfigUtil.getConfiguration().getPath().getEntityPath());

        if(childEntityPackageName != null && childEntityPackageName.replaceAll(" ","").length() > 0){
            String childEntityPackageNameTemp = childEntityPackageName;
            String childEntityPackageNameTemp1 = childEntityPackageName;
            entityPackageName = entityPackageName + childEntityPackageNameTemp.replaceAll("_",".");
            String[] childEntityPackageNameTemps = childEntityPackageNameTemp1.split("_");
            if(childEntityPackageNameTemps.length > 0){
                for (String packageName : childEntityPackageNameTemps) {
                    if("".equals(packageName.replaceAll(" ",""))){
                        continue;
                    }
                    if(filePath.equals(StringUtil.package2Path(ConfigUtil.getConfiguration().getPath().getEntityPath()))){
                        filePath = filePath + packageName;
                    }else{
                        filePath = filePath + File.separator + packageName;
                    }
                }
                FileUtil.mkdirs(filePath);
            }
        }

        Map<String, String> entityData = new HashMap<>();
        entityData.put("BasePackageName", ConfigUtil.getConfiguration().getPackageName());
        entityData.put("EntityPackageName", entityPackageName);
        entityData.put("Author", ConfigUtil.getConfiguration().getAuthor());
        entityData.put("DateTime", new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()));
        entityData.put("ClassName", className);
        if (!StringUtil.isBlank(parentForeignKey)) { // 多对多：主表实体
            entityData.put("Properties", GeneratorUtil.generateEntityProperties(parentClassName, tableInfos));
            entityData.put("Methods", GeneratorUtil.generateEntityMethods(parentClassName, tableInfos));
        } else if (!StringUtil.isBlank(foreignKey)) { // 多对一：主表实体
            entityData.put("Properties", GeneratorUtil.generateEntityProperties(parentClassName, tableInfos, foreignKey));
            entityData.put("Methods", GeneratorUtil.generateEntityMethods(parentClassName, tableInfos, foreignKey));
        } else { // 单表关系
            entityData.put("Properties", GeneratorUtil.generateEntityProperties(tableInfos));
            entityData.put("Methods", GeneratorUtil.generateEntityMethods(tableInfos));
        }

        String fileName = className + ".java";
        String fileName1 = className + "List.java";
        String fileName2 = className + "Search.java";
        // 生成Entity文件
        FileUtil.generateFile(FreemarkerConfigUtils.TYPE_ENTITY, entityData, filePath + File.separator + fileName);
        FileUtil.generateFile(FreemarkerConfigUtils.TYPE_ENTITY_LIST, entityData, filePath + File.separator + fileName1);
        FileUtil.generateFile(FreemarkerConfigUtils.TYPE_ENTITY_SEARCH, entityData, filePath + File.separator + fileName2);
    }
}
